package com.xls.base.exception;

import com.xls.commonutils.Result;
import com.xls.commonutils.ResultCode;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
//import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * spring security的异常
     * @param e
     * @return
     */
    /*@ExceptionHandler(AccessDeniedException.class)
    public Result<String> error(AccessDeniedException e) {
        log.error(e.getMessage());
        return Result.error(ResultCode.NO_PERMISSION);
    }*/

    /**
     * 全局异常处理，所有异常都可以捕获
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result<String> error(Exception e) {
        log.error(e.getMessage());
        if (e.getMessage().equals("不允许访问")) {
            return Result.error(ResultCode.NO_PERMISSION);
        }
        return Result.error();
    }

    /**
     * 自定义异常-业务
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result<String> error(BusinessException e) {
        log.error(e.getErrorMessage());
        return new Result<>(e.getCode(), e.getErrorMessage());
    }


    //@ExceptionHandler(JwtException.class)
    //public Result<String> error(JwtException e) {
    //    log.error(e.getMessage());
    //    return Result.error(ResultCode.INVALID_TOKEN);
    //}

}
